function f= EstimatingTransitionParametersFunctionRFC(params, ProbData, Edata, educ_parent,RorU);
    %  params:   the parameters of the theoretical transition, possibly including rho
    %  ProbData: an array with the data on transitions, where the last dimension is the different regions, indexed by k
    %  Edata: education averages per region
    %  educ_parent: the particular education group for which the estimation is being run
    %  RorU = 1 if Rural, 2 if Favela, 3 if City

    a2 = params(1);
    b2 = params(2);
    a3 = params(3);
    b3 = params(4);
    a4 = params(5);
    b4 = params(6);
    a5 = params(7);
    b5 = params(8);
    j  = educ_parent;  


    if RorU==3, % Brazilian Urban Regions, by Province, 26 in total   
      %% Implied Transition Probabilities from Model for each region k
        for k=1:26  
      %  The Model
                D(1,k)=1;
                D(2,k)=exp( a2+b2*Edata(k,1) );
                D(3,k)=exp( a3+b3*Edata(k,1) );
                D(4,k)=exp( a4+b4*Edata(k,1) );
                D(5,k)=exp( a5+b5*Edata(k,1) );
               Prob_Model(:,k)=D(:,k)./sum(D(:,k));  
       % The Data:
          % First, need to select whethere the region has parents with e(j)
          if sum(ProbData(j,:,k))==1  
             Prob_Data(:,k)= ProbData(j,:,k)';
             I(k)=1;
             fk(k)=sum((Prob_Data(:,k)-Prob_Model(:,k)).^2);
          else,
             I(k)=0;
             fk(k)=0;
          end
        end
     elseif RorU==2, % Brazilian Urban Poor Regions (proxies for Slums), by Province, 26 in total   
      %% Implied Transition Probabilities from Model for each region k
        for k=1:20  
     %  The Model
                D(1,k)=1;
                D(2,k)=exp( a2+b2*Edata(k,2) );
                D(3,k)=exp( a3+b3*Edata(k,2) );
                D(4,k)=exp( a4+b4*Edata(k,2) );
                D(5,k)=exp( a5+b5*Edata(k,2) );
               Prob_Model(:,k)=D(:,k)./sum(D(:,k));
       % The Data:
          % First, need to select whether the region has parents with e(j)
          % If so, compare data with model; otherwise, don't count the region
          
          if sum(ProbData(j,:,k))==1  
             Prob_Data(:,k)= ProbData(j,:,k)';
             I(k)=1;
             fk(k)=sum((Prob_Data(:,k)-Prob_Model(:,k)).^2);
          else,
             I(k)=0;
             fk(k)=0;
          end
        end
        
        
    elseif RorU==1, % Brazilian Rural Regions, by Province, 20 in total   
      %% Implied Transition Probabilities from Model for each region k
        for k=1:20  
     %  The Model
                D(1,k)=1;
                D(2,k)=exp( a2+b2*Edata(k,3) );
                D(3,k)=exp( a3+b3*Edata(k,3) );
                D(4,k)=exp( a4+b4*Edata(k,3) );
                D(5,k)=exp( a5+b5*Edata(k,3) );
               Prob_Model(:,k)=D(:,k)./sum(D(:,k));
       % The Data:
          % First, need to select whether the region has parents with e(j)
          % If so, compare data with model; otherwise, don't count the region
          
          if sum(ProbData(j,:,k))==1  
             Prob_Data(:,k)= ProbData(j,:,k)';
             I(k)=1;
             fk(k)=sum((Prob_Data(:,k)-Prob_Model(:,k)).^2);
          else,
             I(k)=0;
             fk(k)=0;
          end
        end
        
    end
    
    Obs=sum(I(:));
    f=sum(fk)/Obs;
    
    